/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package transactions;

import inquiry.LateChargeInquiry;
import item.Item;
import item.ItemInfo;
import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import myJPA.Model;
import user.Customer;
import user.Employee;

/**
 *
 * @author Nacer Khalil
 */

public class OutstandingLateChargeMgt {
    
    public static boolean addLateCharge(Customer c, Item item, Employee employee)
    {
        try
        {
            java.sql.Date today = new Date(java.util.Calendar.getInstance().getTimeInMillis());
            EntityManager manager = Model.getEntityManager();
            Query q = manager.createNamedQuery("findRentalBycustomerAndItem");
            q.setParameter("cus", c);
            q.setParameter("item", item);
            //q.setParameter("today", today);
            Rental rental = (Rental) q.getResultList().get(0);
            Calendar c1 = Calendar.getInstance();
            c1.setTime(today);
            Calendar c2 = Calendar.getInstance();
            c2.setTime(rental.getDateReturn());
            if(c1.after(c2))
            {
                int days = c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
                q = manager.createNamedQuery("findCharge");
                q.setParameter("type", item.getType());

                ItemInfo info = (ItemInfo) q.getSingleResult();
                int amount = days * info.getLateCharge();

                LateCharge charge = new LateCharge(rental, employee, amount, 0, false, today);
                charge.save();
                return true;
            }
            else
                return false;
                  
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        
        
    }
    
    public static boolean customerhasUnpaidLateCharge(Customer customer)
    {
        EntityManager manager = Model.getEntityManager();
        Query q = manager.createNamedQuery("getLateCharges");
        q.setParameter("c", customer);
        List l = q.getResultList();
        if(l == null || l.size() == 0)
            return false;
        return true;
    }
    
    public static boolean payLateCharge(int lateChargeId, int amount)
    {
        EntityManager manager = Model.getEntityManager();
        LateCharge charge = manager.find(LateCharge.class, lateChargeId);
        if(charge.getAmountPaid() + amount < charge.getAmountCharged())
        {
            charge.setAmountPaid(charge.getAmountPaid() + amount);
        }
        else
        {
            charge.setAmountPaid(charge.getAmountCharged());
            charge.setChargeStatus(true);
        }
        
        charge.update();
        return true;        
    }
    
    public static boolean respondToLateChargeInquiry(int id, String response, Employee employee)
    {
        try
        {
            EntityManager manager = Model.getEntityManager();
            LateChargeInquiry late = manager.find(LateChargeInquiry.class, id);
            late.setEmployee(employee);
            late.setResponse(response);
            
            late.update();
            return true;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
    }
    
    public static boolean cancelLateCharge(int id)
    {
        try
        {
            EntityManager manager = Model.getEntityManager();
            LateCharge late = manager.find(LateCharge.class, id);
            late.remove();
            return true;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
    }
    
   public static String produceOutstandingReport()
   {
       Query query = Model.getEntityManager().createNamedQuery("getAllCustomers");
        List<Customer> list  = (List<Customer>) query.getResultList();
        String report = "";
        if(list == null)
            return report;
        for(Customer c: list)
        {
            report += "customer: "+c.getFullName()+"\n"+lateChargeReport(c)+"\n";
        }
        return report;
   }
   public static String lateChargeReport(Customer customer){
        String result = null; int total = 0;
        java.sql.Date today = new Date(java.util.Calendar.getInstance().getTimeInMillis());
        EntityManager manager = Model.getEntityManager();
        Query q = manager.createNamedQuery("getLateCharges");
        q.setParameter("c", customer);
        List <LateCharge> l = q.getResultList();
        for(int i=0; i<l.size(); i++)
        {
            result += "Title: "+ l.get(i).getRental().getItem().getTitle()+", Return date: "+l.get(i).getRental().getDateReturn()+", amount charged: "+l.get(i).getAmountCharged()+"\n";
            total += l.get(i).getAmountCharged();
        }
        result += "Total: "+total+"\n";
        return result;
      
       
       
       
   }
    
    
    
   
}
